/**
  * Author: Mei
  * Date: 2020-12-13 15:11
  * Description:
  * 容器一般分为序列式、关联式、不定序的容器(Unordered Containers)底层是hashtable(11新加）
  * clock() 这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元（clocktick）数，在MSDN中称之为挂钟时间（wal-clock）；若挂钟时间不可取，则返回-1。其中clock_t是用来保存时间的数据类型。 返回单位是毫秒
  */
#include "../../head.h"

using namespace std;

#define Size 500000

long get_a_target_long() {
    long target = 0;
    cout << "target (0." << endl;
}

int compareLongs(const void *a, const void *b) {
    return (*(long *) a - *(long *) b);
}

int compareStrings(const void *a, const void *b) {
    if (*(string *) a > *(string *) b) {
        return 1;
    } else if (*(string *) a < *(string *) b) {
        return -1;
    } else {
        return 0;
    }
}

int main() {
    forward_list<string> c;
    char buf[10];
    clock_t timeStart = clock();
    for (long i = 0; i < Size; ++i) {
        try {
            snprintf(buf, 10, "%d", rand());
            c.push_front(string(buf));
        } catch (exception &p) {
            cout << "i=" << i << " " << p.what() << endl;
            abort(); //退出
        }
    }
    cout << "mill-seconds: " << (clock() - timeStart) << endl;
    cout << "array.max_size() = " << c.max_size() << endl;
    cout << "array.front() = " << c.front() << endl;
    //cout << "array.back() = " << c.back() << endl;
    //cout << "array.max_size() = " << c.max_size() << endl;
    //排序
    string target = "1421";
    timeStart = clock();
    auto pItem = find(c.begin(), c.end(), target); //::全局函数
    cout << "find()--mill-seconds: " << (clock() - timeStart) << endl;
    if (pItem != c.end()) {
        cout << "found." << *pItem << endl;
    } else {
        cout << "not found!" << endl;
    }

    {
        timeStart = clock();
        //排序
        c.sort(); //调用list中的sort()  如果容器有自己的排序 就用自己的
        cout << "sort()--mill-seconds: " << (clock() - timeStart) << endl;
    }
    /**
     * 执行结果
     *  mill-seconds: 538
        array.max_size() = 461168601842738790
        array.front() = 29794
        find()--mill-seconds: 2
        found.1421
        sort()--mill-seconds: 1734
     *
     */
    return 0;
}